package top.tupobi.flutterdemo.platform_view

import android.content.Context
import android.graphics.Color
import android.os.Build
import android.view.View
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.FrameLayout
import com.google.gson.Gson
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.StandardMessageCodec
import io.flutter.plugin.platform.PlatformView
import io.flutter.plugin.platform.PlatformViewFactory
import top.tupobi.flutterdemo.R
import top.tupobi.flutterdemo.channels.BasePlugin
import top.tupobi.flutterdemo.channels.ChannelEvent
import top.tupobi.flutterdemo.ext.loge


class TestPlatformView(private val mContext: Context, private val mCreateParams: Map<*, *>) :
        BasePlugin("TestPlatformView", "TestPlatformViewEvent"),
        PlatformView {

    private var inflateView: View = View.inflate(mContext, R.layout.activity_vr_look_web, null)

    init {
        val content = inflateView.findViewById<FrameLayout>(R.id.content)
        val webView = WebView(mContext)
        content.addView(webView)

        webView.apply {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                WebView.setWebContentsDebuggingEnabled(true)
            }
            setBackgroundColor(Color.TRANSPARENT)
            background?.alpha = 0
            settings.apply {
                loadWithOverviewMode = true
                setSupportZoom(true)
                builtInZoomControls = true
                displayZoomControls = false
                setAppCacheEnabled(true)
                cacheMode = WebSettings.LOAD_DEFAULT
                useWideViewPort = true
                javaScriptEnabled = true
                domStorageEnabled = true
                databaseEnabled = true
                layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
                }
            }

            webViewClient = object : WebViewClient() {

                override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
                    webView.loadUrl(url)
                    return true;
                }
            }
        }
        webView.loadUrl(mCreateParams["url"] as String)
    }

    override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
        when (call.method) {
            "test" -> {
                "test method call, mCreateParams == $mCreateParams".loge()
                val channelEvent = ChannelEvent("eventCanClick", "uCanClick")
                sendEvent2flutter(Gson().toJson(channelEvent))
            }
        }
    }

    override fun getView(): View {
        return inflateView
    }

    override fun dispose() {
    }

}

class AndroidPlatformViewFactory(private val flutterEngine: FlutterEngine) : PlatformViewFactory(StandardMessageCodec.INSTANCE) {
    /**
     * 创建PlatformView
     * @param context 上下文
     * @param viewId 视图的id
     * @param args flutter端传回的参数
     * @return 返回一个PlatformView的实现类
     */
    override fun create(context: Context, viewId: Int, args: Any): PlatformView {
        val createParams = args.let { args as Map<*, *> }
        val view = TestPlatformView(context, createParams)
        flutterEngine.plugins.add(view)
        return view
    }
}